<template>
  <el-dialog
    title="新規"
    v-model="dialogVisible"
    @close="dialogFormVisible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form size="small" :model="form" ref="form">
      <el-row>
        <el-col :span="12">
          <el-form-item
            label="取引先名"
            :label-width="formLabelWidth"
            :rules="[{ required: true, message: ' ' }]"
            prop="name"
          >
            <el-input
              v-model="form.name"
              autocomplete="off"
              placeholder="取引先名"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="取引先名(カナ)"
            :label-width="formLabelWidth"
            prop="title"
          >
            <el-input
              v-model="form.title"
              autocomplete="off"
              placeholder="取引先名(カナ)"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item
            label="顧客コード"
            :label-width="formLabelWidth"
            prop="number"
          >
            <el-input
              v-model="form.number"
              autocomplete="off"
              placeholder="顧客コード"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="取引先担当者"
            :label-width="formLabelWidth"
            prop="manager"
          >
            <el-input
              v-model="form.manager"
              autocomplete="off"
              placeholder="取引先担当者"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="敬称" :label-width="formLabelWidth">
            <el-radio-group v-model="form.subtitle">
              <el-radio :label="1">御中</el-radio>
              <el-radio :label="2">様</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="メモ" :label-width="formLabelWidth" prop="notes">
            <el-input
              v-model="form.notes"
              autocomplete="off"
              placeholder="メモ"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item
            label="郵便番号"
            :label-width="formLabelWidth"
            prop="postal_code"
          >
            <el-input
              v-model="form.postal_code"
              autocomplete="off"
              placeholder="郵便番号"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="電話番号"
            :label-width="formLabelWidth"
            prop="phone"
          >
            <el-input
              v-model="form.phone"
              autocomplete="off"
              placeholder="電話番号"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item
            label="メール"
            :label-width="formLabelWidth"
            prop="email"
          >
            <el-input
              v-model="form.email"
              autocomplete="off"
              placeholder="メール"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="FAX"
            :label-width="formLabelWidth"
            prop="fax_no"
          >
            <el-input
              v-model="form.fax_no"
              autocomplete="off"
              placeholder="FAX"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item
            label="都道府県"
            :label-width="formLabelWidth"
            prop="city_id"
          >
            <el-select v-model="form.city_id" placeholder="選択してください">
              <el-option
                v-for="item in cityList"
                :key="item.id"
                :label="item.title"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="住所1"
            :label-width="formLabelWidth"
            prop="addressA"
          >
            <el-input
              v-model="form.addressA"
              autocomplete="off"
              placeholder="住所1"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item
            label="住所2"
            :label-width="formLabelWidth"
            prop="addressB"
          >
            <el-input
              v-model="form.addressB"
              autocomplete="off"
              placeholder="住所2"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12"></el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible">キャンセル</el-button>
        <el-button type="primary" @click="addClick()" :loading="loading"
          >確定</el-button
        >
      </div>
    </template>
  </el-dialog>
</template>

<script>
import deptApi from "@/api/dept.js";
import clientApi from "@/api/client.js";
export default {
  data() {
    return {
      form: {
        subtitle: 1,
        meno: "",
      },
      categoryList: [],
      sort: "100",
      radio: "1",
      /*左边长度*/
      formLabelWidth: "120px",
      /*是否显示*/
      dialogVisible: false,
      loading: false,
      cityList: [],
    };
  },
  props: ["open_add"],
  created() {
    this.dialogVisible = this.open_add;
    this.getCityList();
  },
  methods: {
    /*获取列表*/
    getCityList() {
      let self = this;
      let Params = {};
      deptApi
        .citys(Params, true)
        .then((data) => {
          self.loading = false;
          self.cityList = data.data;
        })
        .catch((error) => {
          self.loading = false;
        });
    },
    /*添加插件*/
    addClick() {
      let self = this;
      let params = this.form;
      self.$refs.form.validate((valid) => {
        if (valid) {
          self.loading = true;
          clientApi
            .adds(params, true)
            .then((res) => {
              if (res.code == 1) {
                self.loading = false;
                ElMessage({
                  message: "追加成功",
                  type: "success",
                });
                self.dialogFormVisible(true);
              } else {
                self.loading = false;
              }
            })
            .catch((error) => {});
        }
      });
    },
    /*关闭弹窗*/
    dialogFormVisible(e) {
      if (e) {
        this.$emit("closeDialog", {
          type: "success",
          openDialog: false,
        });
      } else {
        this.$emit("closeDialog", {
          type: "error",
          openDialog: false,
        });
      }
    },
  },
};
</script>

<style></style>
